package com.app.service.impl;

import com.app.mapper.ComRecruitersMapper;
import com.app.model.ComRecruitersModel;
import com.app.service.ComRecruitersService;
import com.app.service.EnterpriseService;
import com.app.service.UserService;
import com.app.vo.ComRecruitersVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Description:用户实现类
 * @Author: N
 * @Date: 2022年11月24日 10点29分
 */
@Service
public class ComRecruitersServiceImpl implements ComRecruitersService {

    @Autowired
    ComRecruitersMapper comRecruitersMapper;

    @Autowired
    EnterpriseService enterpriseService;

    @Autowired
    UserService userService;

    @Override
    public int insert(ComRecruitersModel model) {
        return comRecruitersMapper.insert(model);
    }

    @Override
    public void update(ComRecruitersModel model) {

        comRecruitersMapper.updateById(model);
    }

    @Override
    public void delete(int id) {
        comRecruitersMapper.deleteById(id);
    }

    @Override
    public ComRecruitersModel getComRecruitersById(int id) {
        QueryWrapper<ComRecruitersModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","real_name","com_position","com_id").eq("id", id);
        List<ComRecruitersModel> userList = comRecruitersMapper.selectList(queryWrapper);
        if (userList==null) {
            return null;
        } else {
            return userList.get(0);
        }
    }

    @Override
    public List<ComRecruitersModel> getComRecruitersByComId(String comIds[]) {
        QueryWrapper<ComRecruitersModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","real_name","com_id").in("com_id", comIds);
        List<ComRecruitersModel> userList = comRecruitersMapper.selectList(queryWrapper);
        return userList;
    }

    @Override
    public List<ComRecruitersModel> getComRecruitersList(ComRecruitersModel model) {

        return comRecruitersMapper.getComRecruitersList(model);
    }

    @Override
    public int getModelListCount(ComRecruitersModel model) {
        return comRecruitersMapper.getModelListCount(model);
    }

    @Override
    public int getCountAll() {
        return comRecruitersMapper.selectCount(null);
    }

    @Override
    public int getCountByComId(Integer comId) {
        QueryWrapper<ComRecruitersModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_id",comId);
        return comRecruitersMapper.selectCount(queryWrapper);
    }

    @Override
    public int getCountByGroup(String startTime,String endTime,Integer type) {
        QueryWrapper<ComRecruitersModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DISTINCT com_id");
        if(type==1)
        {
            queryWrapper.apply(" TO_DAYS(up_time) >= TO_DAYS('"+startTime+"')");
            queryWrapper.apply(" TO_DAYS(up_time) <= TO_DAYS('"+endTime+"')");
        }else{
            queryWrapper.apply(" TO_DAYS(up_time) > TO_DAYS('2023-07-14 00:00:00')");
        }

        return comRecruitersMapper.selectCount(queryWrapper);
    }

    @Override
    public int getNotPublishCount() {
        QueryWrapper<ComRecruitersModel> queryWrapper = new QueryWrapper();
        queryWrapper.apply("id not in(select rec_id from tb_job_publish)");
        Integer count = comRecruitersMapper.selectCount(queryWrapper);
        return count;

    }

    @Override
    public ComRecruitersVo personnelList(ComRecruitersModel model) {
        List<ComRecruitersModel> modelList = getComRecruitersList(model);
        for (int i = 0; i < modelList.size(); i++) {
            modelList.get(i).setEnterprise(enterpriseService.findModelById(modelList.get(i).getComId()));
            if (modelList.get(i).getRRegisterState() == 4) {
                modelList.get(i).setStateText("已拒绝");
            } else if (modelList.get(i).getRRegisterState() == 3) {
                modelList.get(i).setStateText("待审核");
            } else if (modelList.get(i).getRRegisterState() == 9) {
                modelList.get(i).setStateText("已通过");
            }
            modelList.get(i).setUser(userService.getUserByRId(modelList.get(i).getId()));
        }
        int count = getModelListCount(model);
        ComRecruitersVo vo = new ComRecruitersVo();
        vo.setList(modelList);
        vo.setCount(count);
        return vo;
    }
}
